package com.taritari.journal.controller;


import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.taritari.journal.entity.po.TestPo;
import com.taritari.journal.services.TestService;
import com.taritari.journal.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author taritari
 * @date 2023-12-02 17:12
 * @description
 */
@RestController
@Slf4j
@Tag(name = "测试Controller", description = "这里进行功能和连接的测试")
@RequestMapping("/test")
public class TestController {
    @Autowired
    private TestService testService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/hello")
    @Operation(summary = "返回hello的测试接口", description = "return hello")
    public Result show() {
        return Result.success("Hello Spring Boot 3 !");
    }

    @GetMapping("/test")
    @Operation(summary = "返回测试数据库信息", description = "对mybatisplus的api调用测试")
    public Result getEricUserList() {
        List<TestPo> list = testService.list();
        return Result.success(list);
    }

    @GetMapping("/selectList")
    @Operation(summary = "测试xml文件的自定义sql", description = "测试xml文件的自定义sql")
    public Result selectList() {
        List<TestPo> testPos = testService.selectList();
        return Result.success(testPos);
    }

    @GetMapping("/redisGetTest")
    @Operation(summary = "测试redis的get", description = "测试redis的get")
    public Result redisGetTest() {
        stringRedisTemplate.opsForValue().set("test", "111");
        String test = stringRedisTemplate.opsForValue().get("test");
        return Result.success(test);
    }

    public void makeOrderDirect(String userId, String productId, int num) {
        String exchangeName = "amq.direct";
        String routingKey = "";
        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生成成功:" + orderId);
        rabbitTemplate.convertAndSend(exchangeName, "test", orderId);
    }

    @GetMapping("/get-image")
    @Operation(summary = "sd文生图", description = "sd文生图")
    public ResponseEntity<String> getImage() {
        String url = "http://127.0.0.1:7860/sdapi/v1/txt2img";
        Map<String,Object> override_settings = new HashMap<>();
        override_settings.put("sd_model_checkpoint","pastelMixStylizedAnime_pastelMixPrunedFP16.safetensors");
        override_settings.put("CLIP_stop_at_last_layers",2);
        Map<String,Object> map = new HashMap<>();
        map.put("override_settings",override_settings);
        map.put("negative_prompt","negative_prompt");
        map.put("prompt","rock climbing, challenge, failure, strength, perseverance, determination, success");
        map.put("steps",30);
        map.put("sampler_name","Euler a");
        map.put("width",512);
        map.put("height",512);
        map.put("batch_size",1);
        map.put("n_iter",1);
        map.put("seed",1);
        map.put("CLIP_stop_at_last_layers",2);
        map.put("restore_faces","False");
        String jsonStr = JSONUtil.toJsonStr(map);

        String result = HttpRequest.post(url)
                .body(jsonStr)
                .header("Content-Type", "application/json")
                .execute()
                .body();

        JSONObject jsonObject = JSONUtil.parseObj(result);
        JSONArray images = jsonObject.getJSONArray("images");
        String base64Image =  images.getStr(0);

        // 返回Base64编码的图片给前端
        return ResponseEntity.ok(base64Image);


    }
}
